package com.monkeygroup.mygoplus.view.home.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout;
import android.graphics.Bitmap;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.thing.vo.ZoneVO;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;

/**
 * 選擇圖片項目
 * Created by cumt on 2017/5/27.
 */

public class PublishedChooseImageActivity extends BaseActivity implements View.OnClickListener{
    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PublishedChooseImageActivity.class);

        activity.startActivityForResult(intent,1);
    }

    private List<String> leftListItems;

    private Activity activity;
    private ImageView imgBack;
    private TextView txtTitle;

    private ListView publishImageList;

    private PopupWindow pop;
    private String type="0";



    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.published_choose_image);
    }

    @Override
    public void initView() {

        activity = PublishedChooseImageActivity.this;
        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("選擇上傳項目");

        leftListItems = new ArrayList<String>();

        publishImageList= (ListView) findViewById(R.id.publish_choose_image);



        initData();
        initPopwindow();
    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

    }


    private void initData(){

        LeftAdapter adapter = new LeftAdapter(PublishedChooseImageActivity.this);
        publishImageList.setAdapter(adapter);

        leftListItems.add("室內實景圖");
        leftListItems.add("社區實景圖");
        leftListItems.add("效果圖");
        leftListItems.add("規劃圖");
        leftListItems.add("位置圖");
        leftListItems.add("生活機能圖");
        leftListItems.add("樣品屋圖");
//        leftListItems.add("格局圖");
    }


    private void initPopwindow(){
        LayoutInflater myinflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView=myinflater.inflate(R.layout.popwindow_choose, null);

        pop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        pop.setTouchable(true);
        //  ColorDrawable dw = new ColorDrawable(getResources().getColor(R.color.backgroud_light_I));
        pop.setBackgroundDrawable(new BitmapDrawable());

        LinearLayout llCamera =  (LinearLayout)contentView.findViewById(R.id.ll_camera);
        LinearLayout llAlbum =  (LinearLayout)contentView.findViewById(R.id.ll_album);
        LinearLayout llChooseCancel =  (LinearLayout)contentView.findViewById(R.id.ll_choose_cancel);

        llCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        llAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageActivity.gotoActivity(PublishedChooseImageActivity.this,"1");

            }
        });
        llChooseCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });


    }

    public class LeftAdapter extends BaseAdapter {


        private LayoutInflater mInflater;


        public LeftAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return leftListItems.size();
        }

        @Override
        public Object getItem(int position) {
            return leftListItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View myconvertView, ViewGroup parent) {

            View leftconvertView = mInflater.inflate(R.layout.published_second_item, null);
            final String obj = leftListItems.get(position);
            final TextView txtName = (TextView) leftconvertView
                    .findViewById(R.id.txt_publish_content);
            txtName.setText(obj);


            leftconvertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if(position==0){
                        type="6";
                    }
                    else if(position==1){
                        type="0";
                    }
                    else if(position==2){
                        type="1";
                    }
                    else if(position==3){
                        type="2";
                    }
                    else if(position==4){
                        type="3";
                    }
                    else if(position==5){
                        type="4";
                    }
                    else if(position==6){
                        type="5";
                    }

                    if (pop != null && pop.isShowing()) {
                        pop.dismiss();


                    }
                    else {

                       // pop.showAtLocation(getCurrentFocus(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                        pop.showAsDropDown(txtTitle);
                    }


                }
            });

            return leftconvertView;
        }

    }

    private void initRequest(){


    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {

            String selectedImages = data.getStringExtra("selectedImages");
            String height = data.getStringExtra("height");
            String width = data.getStringExtra("width");
            String size = data.getStringExtra("size");
            String name = data.getStringExtra("name");

            Intent intent=new Intent();
            intent.putExtra("selectedImages", selectedImages.toString());
            intent.putExtra("type", type);

            intent.putExtra("height",height.toString());
            intent.putExtra("width",width.toString());
            intent.putExtra("size",size.toString());
            intent.putExtra("name",name.toString());
            PublishedChooseImageActivity.this.setResult(RESULT_OK,intent);

            if (pop != null && pop.isShowing()) {
                pop.dismiss();


            }
            finish();

        }


    }
}
